//
//  TaskWithGetVersionUpdate.m
//  DuBai
//
//  Created by darktm on 14-3-17.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "TaskWithGetVersionUpdate.h"
#import "DBManager.h"

#import "Brand.h"

@implementation TaskWithGetVersionUpdate

-(void)prepare{
    NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
    self.versionNo = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"dbversion"]];
    if(self.versionNo == nil || !self.versionNo.length){
        self.versionNo = @"0";
        [mDic setValue:@"0" forKey:@"versionNo"];
    }else{
        [mDic setValue:self.versionNo forKey:@"versionNo"];
    }
    [self startAsyncWithParams:mDic withMethod:@"GET" withURL:[Configure getUpdateData]];
}
-(id)parese{
    
    NSMutableArray *ret = [NSMutableArray array];
    
    WYDatabase *db = [[DBManager shareInstance:DBTypeCn] db];
    if([self.rcvData[@"version"] integerValue] > [self.versionNo integerValue]){
        //sql
        NSArray *a = self.rcvData[@"sqls"];
        
//        [[[DBManager shareInstance:DBTypeCn] db] beginTransaction];
        
        for (NSString *sql in a) {
            [db createTableWithSql:sql];
        }
        
        [[NSUserDefaults standardUserDefaults] setValue:self.rcvData[@"version"] forKey:@"dbversion"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
    ret = [db queryObjWithClass:[Brand class]];
    
    return ret;
}


@end
